home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 19 / ctopas2.zip / CTOPAS.C next >
Text File  |  1991-01-10  |  3KB  |  136 lines

  1. /* C to Pascal program -- filter to replace C punctuation and certain
  2.    key words with their Pascal equivalents.
  3.  
  4.   C form          Pascal form
  5.  -======-        -===========-
  6.     "                 '
  7.     {               BEGIN
  8.     }                END
  9.   <tab>        < 2 blank spaces >
  10.     ()            < nothing >
  11.     &&               AND
  12.     ||                OR
  13. comment start        {
  14. comment end          }
  15.     ==               =
  16.     !=               <>
  17.     =                :=
  18.  printf            writeln
  19.  scanf             readln
  20.  while             WHILE
  21.  
  22. Usage:  ctopas <infile >outfile
  23. */
  24.  
  25. #include "stdio.h"
  26. #include "ctype.h"
  27. #define EOF -1
  28. #define EOS '\0'
  29.  
  30. main()
  31. {
  32.    char c, *letter, word[100];
  33.    int wordlnth;
  34.  
  35.    letter=word;
  36.    wordlnth=0;
  37.    while((c=getchar()) != EOF) {
  38.       if(isalpha(c)) letter[wordlnth++]=c;
  39.       else {
  40.          if(wordlnth>0) {            /* word ready to check */
  41.             letter[wordlnth]='\0';
  42.             wtest(word);             /* pass or replace it */
  43.             wordlnth=0;              /* reset index */
  44.          }
  45.          ctest(c);                   /* process following character */
  46.       }
  47.    }
  48. }   /* Note:  the last word in the file will be missed if it is immediately
  49. followed by EOF with no intervening nonalphanumeric characters.  This is not
  50. a problem for Pascal or C source files.  */
  51.  
  52. wtest(word)
  53. char *word;
  54. {
  55.    char *swapword;
  56.  
  57.    swapword=word;
  58.    switch(word[0]) {       /* test first letter, then rest of word */
  59.       case 'p':  if(strcmp(word,"printf\0")==0) swapword="writeln\0";
  60.         break;
  61.       case 's':  if(strcmp(word,"scanf\0")==0) swapword="readln\0";
  62.         break;
  63.       case 'w':  if(strcmp(word,"while\0")==0) swapword="WHILE\0";
  64.         break;
  65.       default:  break;       /* pass unchanged */
  66.    }
  67.    swap(swapword);
  68. }
  69.  
  70. ctest(c)
  71. char c;
  72. {
  73.    switch(c) {
  74.    case '"':  putchar('\'');
  75.      break;
  76.    case '{':  swap("BEGIN\0");
  77.      break;
  78.    case '}':  swap("END;\0");
  79.      break;
  80.    case '\t':  swap("  \0");
  81.      break;
  82.    case '&':  swapif('&','&',"AND  \0");
  83.      break;
  84.    case '|':  swapif('|','|',"OR  \0");
  85.      break;
  86.    case '(':  swapif('(',')',"\0");
  87.      break;
  88.    case '/':  swapif('/','*',"{\0");
  89.      break;
  90.    case '*':  swapif('*','/',"}\0");
  91.      break;
  92.    case '!':  swapif('!','=',"<>\0");
  93.      break;
  94.    case '<':
  95.    case '>':  putchar(c);      /* <x and >x are passed unchanged */
  96.               c=getchar();
  97.               putchar(c);
  98.      break;
  99.    case '=':  identassign();   /* == -> =, = -> := */
  100.      break;
  101.    default :  putchar(c);
  102.      break;
  103.  
  104.    }
  105. }
  106.  
  107. swap(s)
  108. char *s;
  109. {
  110.    while(*s != EOS) putchar(*s++);
  111. }
  112.  
  113. swapif(first, second, replacement)
  114. char first, second, *replacement;
  115. {
  116.    char c;
  117.  
  118.    if((c=getchar()) == second) swap(replacement);
  119.    else {
  120.       putchar(first);
  121.       putchar(c);
  122.    }
  123.  
  124. }
  125.  
  126. identassign()
  127. {
  128.    char c;
  129.  
  130.    if((c=getchar()) != '=') {          /* assignment */
  131.       putchar(':');
  132.       putchar('=');
  133.    }
  134.    putchar(c);
  135. }
  136.